package com.example.sys.org.service;

import com.example.core.utils.IdGenerator;
import com.example.core.utils.StringUtils;
import com.example.security.dto.UserAuthInfo;
import com.example.core.mapping.BaseMapping;
import com.example.core.service.BaseServiceImpl;
import com.example.security.service.SysTokenService;
import com.example.core.utils.UserUtils;
import com.example.sys.address.po.SysAddress;
import com.example.sys.address.service.SysAddressService;
import com.example.sys.log.enums.SysLogMethod;
import com.example.sys.log.service.SysLogService;
import com.example.sys.org.constant.OrgConstant;
import com.example.sys.org.dao.SysRoleDao;
import com.example.sys.org.dao.SysUserDao;
import com.example.sys.org.mapping.SysUserMapping;
import com.example.sys.org.po.SysElement;
import com.example.sys.org.po.SysRole;
import com.example.sys.org.po.SysUser;
import com.example.sys.org.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUser, SysUserVo> implements SysUserService, SysElementRelationService {

    @Resource
    private SysRoleDao sysRoleDao;

    @Resource
    private SysLogService sysLogService;

    private SysElementService sysElementService;

    private SysTokenService sysTokenService;

    private SysAddressService sysAddressService;

    @Override
    public Class<? extends BaseMapping> getMappingClass() {
        return SysUserMapping.class;
    }

    public SysAddressService getSysAddressService() {
        if (sysAddressService == null) {
            sysAddressService = getApplicationContext().getBean(SysAddressService.class);
        }
        return sysAddressService;
    }

    public SysTokenService getSysTokenService() {
        if (sysTokenService == null) {
            sysTokenService = getApplicationContext().getBean(SysTokenService.class);
        }
        return sysTokenService;
    }

    public SysElementService getSysElementService() {
        if (sysElementService == null) {
            sysElementService = getApplicationContext().getBean(SysElementService.class);
        }
        return sysElementService;
    }

    /***
     * 根据登录名查询用户信息
     * @param userName
     * @return
     */
    @Override
    public SysUser getUserByLoginName(String userName) {
        return getBaseMapper().getUserByLoginName(userName);
    }

    /***
     * 获取用户id的角色列表
     * @param userId
     * @return
     */
    @Override
    public List<SysRole> getRoleByUserId(String userId) {
        return sysRoleDao.getRoleByUserId(userId);
    }


    /***
     * 用户id对应角色信息
     * @param userId
     * @return
     */
    @Override
    public List<String> getRoleCodeByUserId(String userId) {
        return sysRoleDao.getRoleCodeByUserId(userId);
    }


    /***
     * 获取用户id对应的角色信息
     * @param userId
     * @return
     */
    @Override
    public List<String> getRoleIdListByUserId(String userId) {
        return sysRoleDao.getRoleIdListByUserId(userId);
    }


    /***
     * 获取用户的权限信息
     * @param userId
     * @return
     */
    @Override
    public UserAuthInfo getUserInfo(String userId) {
        SysUser sysUser = getUserByLoginName(userId);
        List<SysRole> roleList = getRoleByUserId(userId);
        List<SysRole> roleOpened = sysRoleDao.getRoleOpened();
        return new UserAuthInfo(sysUser, roleList, roleOpened);
    }


    /***
     * 获取当前用户的详细信息
     * @return
     */
    @Override
    public UserAuthInfo getUserInfo() {
        return getUserInfo(UserUtils.getUserId());
    }


    /***
     * 登录并返回token信息
     * @param userName
     * @return
     */
    @Override
    public String login(String userName) {
        SysUser sysUser = getUserByLoginName(userName);
        sysLogService.addLog(sysUser, SysLogMethod.LOGIN);
        return getSysTokenService().createToken(sysUser.getId());
    }

    /***
     *
     * 登出用户并退出
     * @return
     */
    @Override
    public boolean logout() {
        SysUser sysUser = getById(UserUtils.getUserId());
        sysLogService.addLog(sysUser, SysLogMethod.LOGOUT);
        return getSysTokenService().removeToken();
    }


    /***
     * 查询出需要计算年龄的用户信息
     * @return
     */
    @Override
    public List<SysUser> getUserInfoNeedCalculate() {
        return getBaseMapper().getUserInfoNeedCalculate();
    }

    /**
     * 通过openId获取用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public SysUser loadUserByOpenId(String openId) {
        return getBaseMapper().loadUserByOpenId(openId);
    }

    /**
     * 用户简单注册里面生成用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public SysUser registerWxUser(String openId) {
        SysUser sysUser = new SysUser();
        sysUser.setWxId(openId);
        return sysUser;
    }

    @Override
    public List<String> getElementAuthList(String userId) {
        return Arrays.asList(userId);
    }

    /***
     * 用户新增的时候，需要维护
     * @param po
     * @return
     * @throws Exception
     */
    @Override
    public String add(SysUser po) throws Exception {
        if (StringUtils.isEmpty(po.getUserName())) {
            po.setUserName(IdGenerator.generateStringId());
        }
        if (StringUtils.isNotEmpty(po.getAddressId())) {
            SysAddress sysAddress = getSysAddressService().getById(po.getAddressId());
            po.setAddressName(sysAddress.getPath());
        }
        String id = super.add(po);
        SysElement sysElement = new SysElement();
        sysElement.setType(OrgConstant.PERSON);
        BeanUtils.copyProperties(po, sysElement);
        getSysElementService().add(sysElement);
        return id;
    }

    /***
     * 更新用户信息的
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public String update(SysUserVo vo) throws Exception {
        SysUser sysUser = getById(vo.getId());
        if (StringUtils.isNotEmpty(vo.getAddressId())) {
            SysAddress sysAddress = getSysAddressService().getById(vo.getAddressId());
            vo.setAddressName(sysAddress.getPath());
        }
        SysElement sysElement = getSysElementService().getById(vo.getId());
        if (sysElement != null) {
            sysElement.setParentId(sysUser.getParentId());
            sysElement.setParentName(sysUser.getParentName());
            sysElement.setHierarchy(sysUser.getHierarchy());
            sysElement.setName(sysUser.getName());
            getSysElementService().saveOrUpdate(sysElement);
        }
        return super.update(vo);
    }

    /***
     * 用户信息删除并清理数据
     * @param id
     * @throws Exception
     */
    @Override
    public void delete(String id) throws Exception {
        super.delete(id);
        getSysElementService().delete(id);
    }
}
